﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using StudentLookup.BaseEntity;
using System.Net.Http;
using HtmlAgilityPack;
using StudentLookup.Utilities;

namespace StudentLookup.UserControl
{
    public partial class ScheduleDetail : PhoneApplicationPage
    {
        public ScheduleDetail()
        {
            InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string indexSelected = "";
            int index;
            string studentId = "";
            Student student = new Student();
            if (NavigationContext.QueryString.TryGetValue("studentId", out studentId))
            {
                student.StudentId = studentId;
            }

            string htmlString = "";
            using (var client = new HttpClient())
            {
                htmlString = await client.GetStringAsync(Constant.URL_ScheduleExam + student.StudentId);
            }
            try
            {
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(htmlString);
                List<ScheduleExamination> listSchedule = new List<ScheduleExamination>();
                foreach (var item in htmlDocument.DocumentNode.SelectNodes("//tr[@class='kTableRow'] | //tr[@class='kTableAltRow']"))
                {
                    ScheduleExamination scheduleExam = new ScheduleExamination();
                    scheduleExam.CourseName = item.SelectSingleNode(".//td[2]").InnerText.Trim();
                    string dayOf = item.SelectSingleNode(".//td[3]").InnerText.Trim();
                    string date = item.SelectSingleNode(".//td[4]").InnerText.Trim();
                    scheduleExam.ScheduleExamDate = dayOf + " " + date;
                    scheduleExam.ScheduleExamShift = item.SelectSingleNode(".//td[5]").InnerText.Trim();
                    scheduleExam.StudentNumber = int.Parse(item.SelectSingleNode(".//td[6]").InnerText.Trim());
                    scheduleExam.ScheduleExamTurn = byte.Parse(item.SelectSingleNode(".//td[7]").InnerText.Trim());
                    scheduleExam.ScheduleExamRoom = item.SelectSingleNode(".//td[8]").InnerText.Trim();
                    scheduleExam.ScheduleExamLocation = item.SelectSingleNode(".//td[9]").InnerText.Trim();
                    scheduleExam.ScheduleExamFee = item.SelectSingleNode(".//td[10]").InnerText.Trim();
                    scheduleExam.Note = item.SelectSingleNode(".//td[11]").InnerText.Trim();

                    listSchedule.Add(scheduleExam);
                }
                if (NavigationContext.QueryString.TryGetValue("indexSelected", out indexSelected))
                {
                    index = int.Parse(indexSelected);
                    txtCourseName.Text = Library.LimitWords(listSchedule[index].CourseName, 12, " ...");
                    txtDate.Text = listSchedule[index].ScheduleExamDate;
                    txtShift.Text = listSchedule[index].ScheduleExamShift;
                    txtNumber.Text = listSchedule[index].StudentNumber.ToString();
                    txtTurn.Text = listSchedule[index].ScheduleExamTurn.ToString();
                    txtRoom.Text = listSchedule[index].ScheduleExamRoom;
                    txtLocation.Text = listSchedule[index].ScheduleExamLocation;
                    txtFee.Text =Library.RemoveSpecialCharacter(listSchedule[index].ScheduleExamFee);
                }
            }
            catch (Exception ex) 
            { 
                MessageBox.Show(ex.Message);
            }
        }
    }
}